from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
#from django_whoosh.managers import WhooshManager
import datetime

def get_image_path(instance, filename):
    return 'itemphotos/%s/%s' % (instance.Owner, filename)
def get_image_path_user(instance, filename):
    return 'userphotos/%s/%s' % (instance.UserId, filename)


class Tag(models.Model):
  name = models.CharField(max_length=64, unique=True)
  def __unicode__(self):
        return self.name
class Item(models.Model):
    Id=models.AutoField('Id', primary_key=True)
    Name=models.CharField(name="ItemName",max_length=100)
    date = models.DateTimeField(auto_now_add=True)
    MinStartBid=models.FloatField(name="StartPrice")
    Description=models.TextField(name="Description",max_length=2000)
    Owner=models.ForeignKey(User,related_name="Placed_by")
    Tags=models.ManyToManyField(Tag,blank=True)
    Status=models.BooleanField(name="IsBidding",default=True)
    #objects = WhooshManager('Name', fields=['Name', 'Description'])
    image = models.ImageField(blank=False, upload_to=get_image_path)
    class Meta:
        get_latest_by = '-date'
    def get_absolute_url(self):
        return '/auction/item/%d/' % self.id
    def __unicode__(self):
        return self.Name
    #def get_upload_to(self, field_attname):
    #    """Get upload_to path specific to this photo."""
    #    return 'images/%d' % self.Owner
	

class WinningBid(models.Model):
	Id=models.AutoField('ID', primary_key=True)
	ItemId=models.ForeignKey(Item)
	Placed_by=models.ForeignKey(User)
	CurrentwinningBid=models.FloatField(name="winning bid")
	Max_Offered=models.FloatField(name="MaxOffered")
	def __unicode__(self):
		return '%s is bidded by %s' % (self.ItemId,self.Placed_by)
class user_details(models.Model):
	UserId=models.ForeignKey(User)
	fname=models.CharField(name="FirstName",max_length=50)
	mname=models.CharField(name="MnameName",max_length=50)
	lname=models.CharField(name="LastName",max_length=50)
	address=models.CharField(name="Address",max_length=50)
	country=models.CharField(name="country",max_length=50)
	image = models.ImageField(blank=False, upload_to=get_image_path_user)
	#is_activated=models.BooleanField(name="IsBidding",default=True)
	def __unicode__(self):
		return self.fname